// pages/nome/nome.js
const utils = require('../../utils/util.js')

Page({
  data: {
    checkAll: false,
    labelsList: [],
    editorShow: false,
    labelName: '',
    cardId: ''
  },
  ...utils.defaultShare,
  onLoad: function (options) {
    console.log('options', options)
    this.setData({
      cardId: options.cardId || ''
    })
  },
  onShow() {
    this.getlabelsList()
  },
  getlabelsList(){
    // 查询可用标签
    const list = [{
      id: 1,
      name: '客户',
    }, {
      id: 2,
      name: '供应商',
    }, {
      id: 3,
      name: '合作伙伴',
    }, {
      id: 4,
      name: '同行',
    }, {
      id: 5,
      name: 'VIP',
    }];
    utils
      .ajax("/v1/bcs/getCardLabels.do")
      .then(res => {
        if (res.code === "000000") {
          const list = res.data.labels.map(item=>{
            return {name: item, selected: false}
          })
          this.setData({
            labelsList: list
          })
          this.getSelectedLabels()
        } else {
          console.log(res.message || "获取可用标签失败");
        }
      })
      .catch(res => {
        utils.message(res.message || "网络错误");
      });
  },
  getSelectedLabels(){
    // 查询名片上的标签
    utils
      .ajax("/v1/bcs/queryUserCardLables.do",{
        cardId: this.data.cardId
      })
      .then(res => {
        if (res.code === "000000") {
          let list = [...this.data.labelsList].map(item=>{
            if(res.data.labels.indexOf(item.name)>=0){
              return { name: item.name, selected : true}
            }else{
              return {  name: item.name ,selected: false}
            }
          })
          this.setData({
            labelsList: list
          })
        } else {
          console.log(res.message || "获取可用标签失败");
        }
      })
      .catch(res => {
        utils.message(res.message || "网络错误");
      });
  },
  selectLabel(event){
    const index = event.currentTarget.dataset.index;
    const list = [...this.data.labelsList];
    list[index].selected = !list[index].selected;
    this.setData({
      labelsList: list
    })
  },
  showLabelEditor(){
    this.setData({
      editorShow: true
    })
  },
  editorCancel(){
    this.setData({
      editorShow: false
    })
  },
  editorConfirm(){
    // 发送请求，并把结果添加到labelsList中

    utils
      .ajax("/v1/bcs/addUserCardLabel.do", {
        cardId: this.data.cardId,
        name: this.data.labelName
      })
      .then(res => {
        if (res.code === "000000") {
          const list = [...this.data.labelsList, { name: this.data.labelName, selected: true}]
          this.setData({
            editorShow: false,
            labelName: '',
            labelsList: list
          })
        } else {
          console.log(res.message || "新增标签失败");
        }
      })
      .catch(res => {
        utils.message(res.message || "网络错误");
      });
  },
  labelNameChangeEvent(event){
    const value = event.detail.value;
    this.setData({
      labelName: value
    })
  },
  saveLabelInfo(){
    // 保存分组信息
    const list = [...this.data.labelsList].filter(item=>{
      return item.selected
    }).map(item=>{
      return item.name
    })
    console.log(list);
    utils
      .ajax("/v1/bcs/batchAddUserCardLabel.do", {
        cardId: this.data.cardId,
        names: list
      })
      .then(res => {
        if (res.code === "000000") {
          utils.message("保存成功");
          wx.navigateBack({
            delta: 1
          })
        } else {
          console.log(res.message || "保存标签失败");
          utils.message(res.message || "保存标签失败");
        }
      })
      .catch(res => {
        utils.message(res.message || "网络错误");
      });
    // wx.navigateBack({
    //   delta:1
    // })
  }

})